package com.ecarx.module_network.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.ecarx.module_network.R;
import com.ecarx.module_network.bean.WifiInfo;

import java.util.List;


public class WifiListAdapter extends RecyclerView.Adapter<WifiListAdapter.MyViewHolder> {

    private Context mContext;
    private List<WifiInfo> resultList;
    private onItemClickListener onItemClickListener;

    public void setOnItemClickListener(WifiListAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public WifiListAdapter(Context mContext, List<WifiInfo> resultList) {
        this.mContext = mContext;
        this.resultList = resultList;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_view, parent, false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        final WifiInfo bean = resultList.get(position);
        holder.tvItemWifiName.setText(bean.getSsid());
        holder.tvItemWifiName.setTypeface(null, Typeface.NORMAL);
        //已连接或者正在连接状态的wifi都是处于集合中的首位，所以可以写出如下判断
        if (WifiInfo.ConnectState.CONNECTING == bean.getConnectState()) {
            holder.tvItemWifiStatus.setText(R.string.connecting);
            holder.tvItemWifiStatus.setVisibility(View.VISIBLE);
        } else {
            holder.tvItemWifiStatus.setVisibility(View.GONE);
        }
        holder.imageView.setImageResource(bean.getWifiLevelImage());
        holder.itemView.setOnClickListener((view) -> onItemClickListener.onItemClick(view, position, bean));
    }


    @Override
    public int getItemCount() {
        return resultList.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvItemWifiName, tvItemWifiStatus;
        ImageView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvItemWifiName = (TextView) itemView.findViewById(R.id.left_text);
            tvItemWifiStatus = (TextView) itemView.findViewById(R.id.subTitle);
            imageView = (ImageView) itemView.findViewById(R.id.img_right);
        }

    }

    public interface onItemClickListener {
        void onItemClick(View view, int postion, Object o);
    }

}
